<template>
	<ul class="list">
		<li class="item" 
			v-for="key of letters" 
			:key='key'
			@click="handleClick"
			@touchstart.prevent="handleTouchStart"
			@touchmove="handleTouchMove"
			@touchend="handleTouchEnd"
			:ref="key"
			>{{key}}</li>
	</ul>
</template>

<script>
	export default {
		name:'CityAlphabet',
		props:{
			cities:Object
		},
		data(){
			return{
				touchStatus:false,
				startY:0,
				timer:null
			}
		},
		updated(){
//			减少计算次数,提高性能
			this.startY = this.$refs['A'][0].offsetTop
		},
		methods:{
			handleClick(e){
				console.log(e.target.innerHTML)
//				向父组件触发事件
				this.$emit('change',e.target.innerHTML)
			},
			handleTouchStart(){
				this.touchStatus = true
			},
			handleTouchMove(e){
				if(this.touchStatus){
//					函数节流,减少函数执行次数,提高代码性能
					if(this.timer){
						clearTimeout(this.timer)
					}
					this.timer = setTimeout(() => {
						console.log(this.startY)
						const touchY = e.touches[0].clientY - 79
						console.log(touchY)
						const index = Math.floor((touchY-this.startY)/20)
						console.log(index)
						if(index >= 0 && index < this.letters.length){
							this.$emit('change',this.letters[index])
						}
					},16)
				}
			},
			handleTouchEnd(){
				this.touchStatus = false
			}
		},
		computed:{
			letters(){
				const letters = []
				for(let i in this.cities){
					 letters.push(i)
				}
				return letters
			}
		}
	}
</script>

<style lang="stylus" scoped>
@import '~style/varibles.styl'
.list {
	position: absolute;
	top: 1.58rem;
	right: 0;
	bottom: 0;
	width: .4rem;
	/*background: red;*/
	
	/*竖直方向居中*/
	display: flex;
	flex-direction: column;
	justify-content: center;
	
	.item {
		text-align: center;
		line-height: .4rem;
		color: $BG_COLOR;
	}
	
}

</style>